<template src="./index.html"></template>

<script>
import MedicalRecordCurrentStatusService from "../../../../../_services/medicalRecordCurrentStatus.service";
export default {
  name: "TianDingPatientMeCurrentStateComponent",
  data() {
    return {
      user: {},
      params: {
        StatusID: 0, //状态记录编号
        CurrentStatus: "0", //目前状态 = ['0', '1', '2', '3'],
        IsHasTreatMents: "1", //有无治疗方案 = ['0', '1'],
        PregnancyWay: "0", //受孕方式 = ['0', '1', '2'],
        PregnancyTime: 0, //孕周 当目前状态是已孕时，此参数必填，单位为天 ,
        PregnancyWeek: "2", //孕周
        PregnancyDay: "0", //孕周加多少天
        Medicine: [], //用药 ,
        OtherMedicine: "", //其他用药 ,
        UserID: 0 //病患编号
      },
      showMedicine: false,
      MedicineList: [
        {
          key: 0,
          value: "无",
          isRadio: true,
          check: true,
          show: true
        },
        {
          key: 1,
          value: "阿司匹林",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 2,
          value: "低分子肝素",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 3,
          value: "强的松/美卓乐",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 4,
          value: "地塞米松",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 5,
          value: "孕酮",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 6,
          value: "雌激素",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 7,
          value: "免疫球蛋白",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 8,
          value: "TNF抑制剂(益赛普/强克) ",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 9,
          value: "粒细胞刺激因子(瑞白/赛能)",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 10,
          value: "HCG针",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 11,
          value: "二甲双胍",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 12,
          value: "羟氯喹(纷乐/赛能)",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 13,
          value: "溴隐亭",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 14,
          value: "优甲乐",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 15,
          value: "钙片",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 16,
          value: "叶酸",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 17,
          value: "维生素E",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 18,
          value: "维生素C",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 19,
          value: "维生素D",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 20,
          value: "维生素B6",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 21,
          value: "维生素B12",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 22,
          value: "坤灵丸",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 23,
          value: "爱乐维",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 24,
          value: "环胞素",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 25,
          value: "主动免疫",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 26,
          value: "中药",
          isRadio: false,
          check: false,
          show: true
        }
      ],
      showPregnancy: false,
      PregnancyWeekList: [],
      PregnancyDayList: []
    };
  },
  mounted() {
    this.user = JSON.parse(this.local$.getItem("tdUser"));
    this.utils$.getNumberLabel(this.PregnancyWeekList, 2, 45, "周");
    this.utils$.getNumberLabel(this.PregnancyDayList, 0, 6, "天");
    this.getCurrentStatusByUserID();
  },
  methods: {
    getCurrentStatusByUserID() {
      this.indicator$.open("正在加载...");
      let params = {};
      params.userid = this.user.UserID;
      MedicalRecordCurrentStatusService.getCurrentStatusByUserID(params).then(
        response => {
          if (response && response.Data) {
            response.Data.PregnancyWeek = parseInt(
              response.Data.PregnancyTime / 7
            );
            response.Data.PregnancyDay =
              response.Data.PregnancyTime -
              7 * parseInt(response.Data.PregnancyTime / 7);
            this.params = response.Data;

            this.params.CurrentStatus = this.params.CurrentStatus.toString();
            this.params.IsHasTreatMents = this.params.IsHasTreatMents.toString();
            this.params.PregnancyWay = this.params.PregnancyWay.toString();
            this.params.PregnancyWeek = this.params.PregnancyWeek.toString();
            this.params.PregnancyDay = this.params.PregnancyDay.toString();
            this.utils$.dataFormat(this.params);
          }

          this.params.UserID = this.user.UserID;

          if (this.params.StatusID > 0) {
            this.params.Medicine = this.params.Medicine.split(",");
            this.MedicineList.filter((v, i, a) => {
              let include = this.params.Medicine.includes(v.value);
              if (include) {
                v.check = true;
              } else {
                v.check = false;
              }
              return a;
            });
          }
          this.indicator$.close();
        }
      );
    },
    changeMedicine(event) {
      if (this.showMedicine) {
        this.showMedicine = false;
      } else {
        this.showMedicine = true;
      }
    },
    getMedicine(event) {
      let temp = [];
      this.params.Medicine = [];
      if (event[0] === 0) {
        let item = this.MedicineList.find(i => i.key === event[0]);
        temp.push(item.value);
        this.params.Medicine = temp;
      } else {
        event.forEach(key => {
          let item = this.MedicineList.find(i => i.key === key);
          temp.push(item.value);
        });
        this.params.Medicine = temp;
      }
    },
    getPregnancyWeek(event) {
      this.params.PregnancyTime =
        parseInt(this.params.PregnancyWeek) * 7 +
        parseInt(this.params.PregnancyDay);
    },
    getPregnancyDay(event) {
      this.showPregnancy = false;
      this.params.PregnancyTime =
        parseInt(this.params.PregnancyWeek) * 7 +
        parseInt(this.params.PregnancyDay);
    },
    btnPrev() {
      let menu = JSON.parse(this.local$.getItem("tdMeLeftMenus"));
      menu.prev = "prev";
      this.$emit("change", menu);
    },
    btnSubmit(next) {
      this.indicator$.open("正在保存...");

      console.log(this.params);

      let params = {
        StatusID: this.params.StatusID, //状态记录编号
        CurrentStatus: parseInt(this.params.CurrentStatus), //目前状态 = ['0', '1', '2', '3'],
        IsHasTreatMents: parseInt(this.params.IsHasTreatMents), //有无治疗方案 = ['0', '1'],
        PregnancyWay: parseInt(this.params.PregnancyWay), //受孕方式 = ['0', '1', '2'],
        PregnancyTime: this.params.PregnancyTime, //孕周 当目前状态是已孕时，此参数必填，单位为天 ,
        PregnancyWeek: parseInt(this.params.PregnancyWeek), //孕周
        PregnancyDay: parseInt(this.params.PregnancyDay), //孕周加多少天
        Medicine: this.params.Medicine, //用药 ,
        OtherMedicine: this.params.OtherMedicine, //其他用药 ,
        UserID: this.user.UserID //病患编号
      };

      if (params.StatusID > 0) {
        MedicalRecordCurrentStatusService.updateCurrentStatus(params).then(
          response => {
            this.result(response, next);
          }
        );
      } else {
        MedicalRecordCurrentStatusService.addCurrentStatus(params).then(
          response => {
            this.result(response, next);
          }
        );
      }
    },
    result(response, next) {
      if (response && response.Data > 0) {
        this.toast$("保存成功！");
        if (next !== undefined) {
          let menu = JSON.parse(this.local$.getItem("tdMeLeftMenus"));
          menu.next = "next";
          this.$emit("change", menu);
        }
      } else {
        this.toast$("保存失败！");
      }
      this.getCurrentStatusByUserID();
    }
  }
};
</script>
